package com.atzongmaotan.reggie.service.impl;

import com.atzongmaotan.reggie.common.CustomeException;
import com.atzongmaotan.reggie.entity.Dish;
import com.atzongmaotan.reggie.entity.Setmeal;
import com.atzongmaotan.reggie.mapper.CategoryMapper;
import com.atzongmaotan.reggie.service.CategoryService;
import com.atzongmaotan.reggie.service.DishService;
import com.atzongmaotan.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atzongmaotan.reggie.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * ClassName:    CategoryServiceImpl
 * Package:  com.atzongmaotan.service.impl
 * Description:
 *
 * @Author: interface
 * @Create: 2024/6/12 - 下午11:41
 * @Version: v1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 自定义方法先判断是否关联菜品
     * 是否关联套餐
     * @param id
     */
    @Override
    @Transactional
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<Dish>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        if (dishCount > 0) {
            //已关联菜品,抛出一个业务异常
            throw new CustomeException("当前分类项关联了菜品");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<Setmeal>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);
        if (setmealCount > 0) {
            //已关联套餐
            throw new CustomeException("当前分类项关联了套餐");
        }
        super.removeById(id);
    }
}
